#!/usr/bin/env perl

BEGIN {
	push @::INC,".." if -f "../pv3.pm";
	push @::INC,$::ENV{CODEMGR_LIBS} if exists($::ENV{CODEMGR_LIBS});
}

use pv3;
use Getopt::Long;
use Config;
use File::Basename;
use strict;

$::ARG_pv=undef;
$::ARG_msg=undef;
$::ARG_type=undef;
$::ARG_force=0;

sub usage {
	print "
Usage: pv3_ci [-p DIR] [-m checkin] [-t type] file ...

The pv3_ci allows a new file to be initiated as the first
version under PV3 code control, or allows an existing checked out
file to be checked back in again.

It supports the following command line arguments:

-p   Indicates the file which should be used to store the revision
     history of the files in question.
     for the file in question can be found.
-m   A message to include with the check-in. Recommended since 
     otherwise it will be set to 'No comment'.
-f   Force the check in even if the owner is not the person
     that checked it out.
-t   The type of backend repository - obviously must be supported
     on the current machine. Currently supported values are:

     1 - Gdiff Delta format - fast, but 32bit platforms only
     2 - Xdiff internal - fast, default for UNIX if available.
     3 - Xdiff external - uses xdiff binary commands.
     4 - Compressed complete files, Only type currently supported on Windows.

     Leave blank and the utility will default to the best supported
     type on the current machine. If you intend to migrate the files
     between UNIX/Windows then type 4 is currently the best.

Written by Simon Edwards, (C) 2006-2010 www.advantsys.co.uk
";
	exit 1;
}

if(!GetOptions(
	"pv=s"		=> \$::ARG_pv,
	"msg=s"		=> \$::ARG_msg,
	"force"		=> \$::ARG_force,
	"type=s"	=> \$::ARG_type,
	)) {
	usage;
}

if(defined($::ARG_pv)) {
	if(! -d dirname($::ARG_pv)) {
		mkdir dirname($::ARG_pv),0775 or die("Failed to create directory $::ARG_pv: $!");
	}
}
my %args=();
$::ARG_msg="No comment" if !defined($::ARG_msg);
$args{COMMENT}=$::ARG_msg;
$args{TAGS}=$::ARG_tag if defined($::ARG_tag);
$args{FORMAT}=$::ARG_type if defined($::ARG_type);
$args{FORCE}=1 if $::ARG_force;
@::CHECK_IN_LIST=();

if($Config{osname} =~ /mswin/i) {
	my @F=();
	for my $cpat (@ARGV) {
		push @F,glob($cpat);
	}
	@ARGV=@F;
}

for my $cfile (@ARGV) {
# Check in a file - either new file [no ,pv] or checked [out]

	next if -d $cfile;
	next if $cfile =~ /,pv3$/;
	if(! -f $cfile) {
		print "ERROR: File '$cfile' does not exist - ignoring.\n";
		next;
	}
	push @::CHECK_IN_LIST,$cfile;
}

{
	$args{PVFILE}=$::ARG_pv if defined($::ARG_pv);
	my $newlist;
	my ($res,$pv,$newlist,$unlock)=pv3::pvexists_multiple(FILES => \@::CHECK_IN_LIST,%args);
	if($res<0) {
		for my $cc (@$pv) {
			print "ERROR: $cc - ignoring\n";
			exit(1);
		}
	}
	if(@$pv) {
		for my $cc (@$pv) {
			print "Warning: $cc\n";
		}
	}
	for my $cp (@$newlist) {
		if(defined($cp->[3])) {
			print "Checked in: $cp->[0] - $cp->[2],$cp->[1] (Checked out by '$cp->[3]')\n";
		} else {
			print "Checked in: $cp->[0] - $cp->[2],$cp->[1]\n";
		}
	}
	for my $cp (@$unlock) {
		if(defined($cp->[1])) {
			print "Unlocked  : $cp->[0] (Checked out by '$cp->[1]')\n";
		} else {
			print "Unlocked  : $cp->[0]\n";
		}
	}
	exit(0);
}

